<template>
  <div class="loginContent">
    <!-- <el-button type="primary" class="loginBtn" @click="shwLogin = true">登录</el-button> -->
    <div class="login-body">
      <div style="padding-top: 36px; padding-left: 80px;">
        <img src="/ziluw.png" width="77px" alt="">
      </div>
      <div
        style="padding-left: 96px; color: #fff; padding-top: 39px; display: flex; flex-direction: column; position: relative;">
        <img src="/ziluw.png" width="460px" style="position: absolute; top: -10px; right: 0px" alt="">
        <div class="animate__animated animate__fadeInLeft" style="font-size: 24px;">
          WELCOME TO THE SMART TEACHING<br> ASSISTANT SYSTEM
        </div>
        <div class="animate__animated animate__fadeInLeft" style="padding-top: 24px; font-size: 90px;">
          大模型智慧助教系统<br>子路
        </div>
        <div class="animate__animated animate__fadeInLeft"
          style="padding-top: 24px; display: flex; gap:36px; align-items: center;">
          <button @click="shwLogin = true"
            style=" border: none; background-color: #000; border-radius: 32px; width: 140px; height: 64px; color: #fff; font-size: 20px;">开始使用</button>
          <button @click="goToHelp"
            style=" border: none; background-color: #333; border-radius: 32px; width: 140px; height: 64px; color: #fff; font-size: 20px;">使用帮助</button>
          <div style="color: #cff;font-size: 20px; font-weight: bolder; cursor: pointer;" @click="downloadManual">
            点击下载使用手册
          </div>
        </div>
        <div class="animate__animated animate__fadeInDown"
          style="display: flex; gap: 60px; padding-top: 160px; width: 100%; padding-right: 79px;">
          <div v-for="item in loginShowText"
            style="display: flex; flex-direction: column; gap: 12px; justify-content: space-around;">
            <div style="font-size: 24px; font-weight: bolder;">
              {{ item.title }}
            </div>
            <div style="font-size: 18px; color: #ccc;">
              {{ item.content }}
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 视频区域
    <div class="videos-section">
      <div class="video-container" ref="video1Container">
        <h3>视频一</h3>
        <video ref="video1" class="scroll-video"
          src="http://111.63.183.17:8217/upload/file/2025/05/14/1922497346529153025.mp4" controls muted loop></video>
      </div>

      <div class="video-container" ref="video2Container">
        <h3>视频二</h3>
        <video ref="video2" class="scroll-video"
          src="http://111.63.183.17:8217/upload/file/2025/05/14/1922497525500104705.mp4" controls muted loop></video>
      </div>
    </div> -->

    <!-- <el-row> -->

    <!-- <el-col :lg="16" :md="10" class="left hidden-sm-and-down">
        <img src="@/assets/web/images/login2.png" style="height: 45vh">
      </el-col> -->

    <!-- <el-col :lg="8" :md="14" class="right"> -->

    <el-dialog :visible="shwLogin" :before-close="() => (shwLogin = false)" :close-on-click-modal="false" width="550px">
      <el-tabs v-model="activeName">
        <el-tab-pane label="账号登录" name="account">
          <el-form v-if="activeName === 'account'" ref="postForm" :model="postForm" :rules="loginRules">
            <el-form-item prop="username">
              <el-input v-model="postForm.username" style="width: 100%" placeholder="用户名" prefix-icon="el-icon-user" />
            </el-form-item>

            <el-form-item prop="password">
              <el-input v-model="postForm.password" style="width: 100%" placeholder="密码" type="password"
                prefix-icon="el-icon-lock" show-password />
            </el-form-item>

            <el-form-item prop="captchaValue">
              <yf-captcha ref="captcha" v-model="postForm" />
            </el-form-item>

            <el-form-item>
              <el-button :loading="loading" type="primary" style="width: 100%"
                @click.native.prevent="accountLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane v-if="siteData.props.mobileLogin" label="手机登录" name="mobile">
          <el-form v-if="activeName === 'mobile'" ref="postForm" :model="postForm" :rules="loginRules">
            <el-form-item prop="mobile">
              <el-input v-model="postForm.mobile" style="width: 100%" placeholder="手机号码" prefix-icon="el-icon-mobile" />
            </el-form-item>

            <el-form-item prop="smsCode">
              <sms-input ref="sms" v-model="postForm" :type="2" />
            </el-form-item>

            <el-form-item>
              <el-button :loading="loading" type="primary" style="width: 100%"
                @click.native.prevent="mobileLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

      <div style="text-align: right, line-height: 10px">
        <el-link @click="gotoRegister" v-if="siteData.props.userReg">立即注册</el-link>
        <el-link v-if="siteData.props.mobileLogin" href="/#/forgot" style="margin-left: 10px">忘记密码？</el-link>
      </div>

      <demo-account v-if="isDemo" />

      <div v-if="siteData.h5Host || siteData.mpCode" style="line-height: 35px; margin-top: 10px">
        <div class="title-line">手机端</div>
        <el-button v-if="siteData.h5Host" size="mini" type="primary" plain round @click="showH5Code">H5学员端</el-button>
        <el-button v-if="siteData.mpCode" size="mini" type="primary" plain round @click="showMpCode">小程序学员端</el-button>
      </div>

      <third-login />
    </el-dialog>

    <!-- </el-col> -->

    <!-- </el-row> -->

    <el-dialog :visible.sync="h5Visible" width="340px" style="text-align: center" append-to-body>
      <div class="code-tips">扫码进入H5学员端</div>
      <div style="width: 300px; border: #ddd 1px solid">
        <vue-qr :size="298" :logo-src="siteData.backLogo" :logo-scale="0.2" :text="siteData.h5Host" />
      </div>
    </el-dialog>

    <el-dialog :visible.sync="mpVisible" width="340px" style="text-align: center" append-to-body>
      <div class="code-tips">扫码进入小程序学员端</div>
      <img :src="siteData.mpCode" style="width: 300px; border: #ddd 1px solid" />
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import YfCaptcha from "@/components/Captcha";
import SmsInput from "@/components/SmsInput";
import VueQr from "vue-qr";
import ThirdLogin from "@/views/login/components/ThirdLogin.vue";
import DemoAccount from "@/views/login/components/DemoAccount.vue";

export default {
  components: { DemoAccount, ThirdLogin, SmsInput, YfCaptcha, VueQr },
  data() {
    return {
      isDemo: this.$demo,
      activeName: "account",
      loading: false,
      h5Visible: false,
      mpVisible: false,
      wxVisible: false,
      postForm: {
        smsCode: "",
        captchaKey: "",
        captchaValue: "",
      },
      loginShowText: [
        { title: "大模型知识点抽取", content: "基于课程大纲和课程教材的知识点自动抽取" },
        { title: "自动生成数字人课程", content: "基于知识点的讲稿自动生成和数字人授课" },
        { title: "大模型出题", content: "基于知识点的自动出题组卷" },
        { title: "自动阅卷", content: "学生考试完系统自动阅卷" },
        { title: "试卷分析报告生成", content: "智能生成个人和班级的考试分析报告" }
      ],
      loginRules: {
        username: [{ required: true, trigger: "blur", message: "用户名不能为空" }],
        password: [{ required: true, trigger: "blur", message: "密码不能为空" }],
        captchaValue: [{ required: true, trigger: "blur", message: "验证码不能为空" }],
        smsCode: [{ required: true, trigger: "blur", message: "短信验证码不能为空" }],
        mobile: [{ required: true, trigger: "blur", message: "手机号不能为空" }],
      },
      shwLogin: false,

    };
  },

  computed: {
    ...mapGetters(["siteData"]),
  },

  mounted() {
    window.addEventListener("keydown", this.keyDown);
    this.pingHost("10.21.22.107").then((is_inner) => {
      console.log(`Is inner host: ${is_inner}`);
    });
  },

  destroyed() {
    window.removeEventListener("keydown", this.keyDown, false);
  },

  methods: {
    goToHelp() {
      this.$router.push({ name: "Help" });
    },

    downloadManual() {
      // 创建一个a标签，设置href为PDF文件路径，然后触发点击事件
      const link = document.createElement('a');
      link.href = '/使用手册.pdf';
      link.download = '使用手册.pdf'; // 设置下载的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      this.$message({
        message: '使用手册下载中...',
        type: 'success'
      });
    },

    async pingHost(ip) {
      const url = `http://${ip}:80`;
      const startTime = performance.now();
      try {
        const response = await fetch(url, { method: "HEAD", mode: "no-cors" });
        const endTime = performance.now();
        console.log(`Ping to ${url} took ${endTime - startTime} ms`);
        return response.ok;
      } catch (error) {
        console.error(`Ping to ${url} failed: ${error}`);
        return false;
      }
    },
    showH5Code() {
      this.h5Visible = true;
    },
    showMpCode() {
      this.mpVisible = true;
    },

    async mobileLogin() {
      this.$refs.postForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/mobileLogin", this.postForm)
            .then(() => {
              this.$router.push("/");
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },

    accountLogin() {
      this.$refs.postForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.postForm)
            .then((data) => {
              const id = data.id;
              sessionStorage.setItem("id", id);
              this.$router.push("/");
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },

    studentRegister() {
      this.$router.push({ name: "Register" });
    },

    gotoRegister() {
      this.$message("请联系管理员为您分配账号");
    },
    keyDown(e) {
      if (e.keyCode == 13) {
        if (this.activeName === "account") {
          this.accountLogin();
        }

        if (this.activeName === "mobile") {
          this.mobileLogin();
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.loginContent {
  width: 100vw;
  height: 100vh;
  background: #fff;
  position: relative;
  overflow: hidden;

  .login-body {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    background-image: url("../../assets/login/bg.jpg");
    // 蒙版 50%黑色
    background-blend-mode: multiply;
    background-color: rgba(0, 0, 0, 0.5);
    background-size: cover;
    background-position: center;

    button {
      transition: all 0.3s ease;
      cursor: pointer;

      &:hover {
        transform: translateY(-3px);
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
      }

      &:first-of-type:hover {
        background-color: #fff !important;
        color: #000 !important;
      }

      &:last-of-type:hover {
        background-color: #fff !important;
        color: #000 !important;
      }
    }
  }

  .loginBtn {
    width: 80px;
    height: 40px;
    position: absolute;
    top: 30px;
    right: 130px;
    z-index: 99;
  }

  ::v-deep .el-button--primary {
    background-color: #5a8cff !important;
    border-color: #5a8cff !important;
  }

  ::v-deep .el-button--primary:active,
  ::v-deep.el-button--primary:hover,
  ::v-deep.el-button--primary:focus {
    background-color: #1e5ae7 !important;
    border-color: #1e5ae7 !important;
  }

  // 视频区域样式
  .videos-section {
    width: 100%;
    padding: 40px 0;
    background-color: #f5f7fa;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh; // 确保有足够的空间来滚动
  }

  .video-container {
    width: 80%;
    max-width: 800px;
    margin: 30px 0;
    padding: 20px;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;

    h3 {
      margin: 0 0 15px;
      font-size: 22px;
      color: #333;
      text-align: center;
    }

    &:hover {
      transform: translateY(-5px);
    }
  }

  .scroll-video {
    width: 100%;
    border-radius: 4px;
    display: block;
  }
}

// 响应式样式
@media (max-width: 768px) {
  .loginContent {
    .videos-section {
      padding: 20px 0;
    }

    .video-container {
      width: 95%;
      padding: 15px;

      h3 {
        font-size: 18px;
        margin-bottom: 10px;
      }
    }
  }
}
</style>
